home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12308 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  4.7 KB

  1. Path: beta.nedernet.nl!usenet
  2. From: jos@and.nl (Jos A. Horsmeier)
  3. Newsgroups: comp.lang.c
  4. Subject: How about some theory? (was: Re: "Best fit" algorithm (help))
  5. Date: 30 Mar 1996 13:05:32 GMT
  6. Organization: AND Operations Research B.V.
  7. Message-ID: <4jjbis$p19@beta.nedernet.nl>
  8. References: <APC&7'0'22b6b83'874@peg.apc.org> <4jhlu7$5m2@airdmhor.gen.nz>
  9. NNTP-Posting-Host: klepzeiker.and.nl
  10. Mime-Version: 1.0
  11. Content-Type: Text/Plain; charset=ISO-8859-1
  12. X-Newsreader: WinVN 0.99.5
  13.  
  14. In article <4jhlu7$5m2@airdmhor.gen.nz>, gumboot@airdmhor.gen.nz wrote:
  15. |riox@peg.apc.org:
  16.  
  17. |> I am looking for a "best fit" algorithm. 
  18. |> The problem I have is very similar to finding the best way of fitting the 
  19. |> maximum number of songs on (say) 45 minute tape.
  20.  
  21. |  I used to have a program like that for my C64, the guy that wrote it said
  22. |the most effective method he found was trying random combinations.
  23.  
  24. IMHO your friend didn't look hard enough then. I agree, the original poster
  25. was a bit ambiguous when he posed the question (not shown above), but still, 
  26. we're talking about optimization here. The original question implied two
  27. optimization problems:
  28.  
  29. 1: how to arrange for the maximum _number_ of songs on a tape;
  30. 2: how to _minimize_ the amount of unused tape.
  31.  
  32. The first question is easy to solve: start recording the shortest songs
  33. and proceed recording the next song in the row (sorted) until the song to
  34. be recorded doesn't fit on the tape anymore.
  35.  
  36. A random search (as your friend proposed) doesn't bring you much; it's like
  37. wandering around in a foggy mountain landscape, searching for the steepest
  38. valley; Walking around randomly involves 'uphill steps' with no indication
  39. whether or not that step would do any good in finding the 'optimal', 
  40. solution, i.e. finding the steepest valley. On the other hand: going
  41. 'downhill' randomly doesn't buy you much either;  you might end up
  42. in a valley far away (and far above) the 'optimal' valley.
  43.  
  44. Even a 'steepest downhill' step doesn't help you out unless the problem
  45. satisfies quite a nasty criterion: it has to be convex. The first problem
  46. happens to be convex and a steepest downhill step (maximizing the amount
  47. of unused tape after recording a song) brings you down to the deepest
  48. valley, i.e. the maximum _number_ of songs recorded on tape, as fast as 
  49. possible.
  50.  
  51. The second problem is much more 'fun' (mind the quotes, only operations
  52. research afficionados would leave out the quotes ;-) On first inspection
  53. of the problem, there's nothing else we can do but enumerate all possible
  54. combinations of songs and see which one leaves the minimum amount of
  55. unused tape left. Luckily enough, those same or-afficionados took a second
  56. look and came up with the following idea:
  57.  
  58. - let d be the duration of the tape;
  59. - let l[i] be the length of the i-th song;
  60. - if somebody told us that song i happens to be an in the optimal set of
  61.   songs, then we can take advantage of it, by solving a smaller problem,
  62.   i.e. the duration of tape equals d-l[i] and the number of songs is
  63.   reduced by one (song i is removed from the problem set).
  64.  
  65. The last observation brings us back to square one again. The only
  66. advantage we have is, that we're dealing with a smaller problem here.
  67. If we apply the same reasoning to this problem again (did anyone say
  68. 'recursion'?) we end up with a very simple problem; we either have
  69. no songs left and (possibly) a bit of tape left or we have a song left
  70. and not enough tape left to record that song.
  71.  
  72. Let's sprinkle some math lingo over this little problem:
  73.  
  74. let d be the duration of the tape and let S be the set of songs. Let
  75. l[i] be the duration of the i-th song again. Our problem can be denoted
  76. as O(d, S) (the 'O' stands for 'optimal' here, not for 'big-oh').
  77.  
  78. Applying the observations described above, we can deduce:
  79.  
  80.     O(d, S) = min (for all i where l[i] <= d: O(d-l[i], S-i))
  81.  
  82. The recurrent expression shown above doesn't take that much programming
  83. skill to implement. (I've already given the corresponding C functions
  84. in another reply). But here's my question: why don't people try to 
  85. utilize some theory they've been forced to study when they were students,
  86. before trying to implement algorithms that are supposed to do the job?
  87.  
  88. Don't people read books anymore? Do people like to jump in front of one
  89. of those silly terminals/PCs/work stations/whatever right after listening
  90. or reading about the problem to be solved without _thinking_ about it
  91. a bit before starting typing 'void main()' or whatever and moving that
  92. silly mouse all over their desk as if it were their favority Dinky Toy? 
  93. I'm quite pessimistic about the answer to these questions ...
  94.  
  95. Or maybe I'm just getting old ... (no Dik, don't answer ;-)
  96.  
  97. kind regards,
  98.  
  99. Jos aka jos@and.nl
  100. -- 
  101. Atnwgqkrl gy zit vgksr, ug qshiqwtzoeqs!
  102.  
  103.